package com.example.shouybl.fragment;


import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.TypeReference;
import com.example.shouybl.BaseApp;
import com.example.shouybl.R;
import com.example.shouybl.adapter.NewAwardAdapter;
import com.example.shouybl.bean.AppBaseBean;
import com.example.shouybl.bean.NewAward;
import com.example.shouybl.constant.Path;
import com.example.shouybl.utils.AppAsyncTaskHelper;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewAwardFragment extends Fragment {

    public static final String ARGS_KEY="type";
    private PullToRefreshListView ptrNewAward;
    private ListView lvNewAward;
    private ArrayList<NewAward> newAwards=new ArrayList<>();
    private NewAwardAdapter awardAdapter;
    private int page=1;
    private String type;


    public static NewAwardFragment newInstance(String type) {

        Bundle args = new Bundle();
        args.putString(ARGS_KEY,type);
        NewAwardFragment fragment = new NewAwardFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type=getArguments().getString(ARGS_KEY);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v= inflater.inflate(R.layout.fragment_new_award, container, false);
        initView(v);
        initData();
        lvNewAward.setAdapter(awardAdapter);
        return v;
    }

    private void initData() {
        TypeReference<AppBaseBean<NewAward>> typeReference=new TypeReference<AppBaseBean<NewAward>>(){};
        AppAsyncTaskHelper.getInstance().post(Path.PATH_AWARD, getParams(), newAwards, ptrNewAward, awardAdapter, page, typeReference);
    }

    private void initView(View v) {
        ptrNewAward = (PullToRefreshListView) v.findViewById(R.id.ptr_newaward);
        ptrNewAward.setMode(PullToRefreshBase.Mode.BOTH);
        lvNewAward = ptrNewAward.getRefreshableView();
        awardAdapter=new NewAwardAdapter(newAwards, BaseApp.getInstance());

    }


    public RequestParams getParams() {
        RequestParams params=new RequestParams();
        params.add("type",type);
        params.add("page",String.valueOf(page));
        return params;
    }
}
